package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.constant.ProduceErrorCode;
import com.ruibang.glass.produce.domain.req.ProcessAbnormalityReq;
import com.ruibang.glass.produce.entity.ProcessAbnormality;
import com.ruibang.glass.produce.mapper.ProcessAbnormalityMapper;
import com.ruibang.glass.produce.service.ProcessAbnormalityService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: songJinKang
 * @CreateTime: 2023-11-22  20:19
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class ProcessAbnormalityServiceImpl extends ServiceImpl<ProcessAbnormalityMapper, ProcessAbnormality> implements ProcessAbnormalityService {


    @Autowired
    private RedisSingleService redisService;

    @Override
    public ResultBody add(ProcessAbnormality processAbnormality) {
        if (processAbnormality == null) {
            return ResultBody.fail(ProduceErrorCode.PARAM_IS_EMPTY.getCode(), ProduceErrorCode.PARAM_IS_EMPTY.getMsg());
        }
        processAbnormality.setIsDelete(Constants.ZERO_STR);
        this.save(processAbnormality);
        return ResultBody.success();
    }

    @Override
    public ResultBody edit(ProcessAbnormality processAbnormality) {
        if (processAbnormality == null || StringUtils.isBlank(processAbnormality.getAbnormalityId())) {
            return ResultBody.fail(ProduceErrorCode.PARAM_IS_EMPTY.getCode(), ProduceErrorCode.PARAM_IS_EMPTY.getMsg());
        }
        this.updateById(processAbnormality);
        return ResultBody.success();
    }

    @Override
    public IPage<ProcessAbnormality> queryAbnormality(ProcessAbnormalityReq processAbnormalityReq) {
        IPage<ProcessAbnormality> page = baseMapper.queryAbnormality(new Page<>(processAbnormalityReq.getPageNo(), processAbnormalityReq.getPageSize()), processAbnormalityReq);
        if (page!=null&& CollectionUtils.isNotEmpty(page.getRecords())){
            for (ProcessAbnormality processAbnormality : page.getRecords()) {
                setUserName(processAbnormality);
            }
        }
        return page;
    }

    @Override
    public void deleteById(String id) {
        lambdaUpdate().eq(ProcessAbnormality::getAbnormalityId, id).set(ProcessAbnormality::getIsDelete, Constants.ONE_STR).update();
    }

    private void setUserName(ProcessAbnormality processAbnormality) {
        String updateUserId = processAbnormality.getUpdateUserId();
        String recorderUserId = processAbnormality.getRecorderUserId();
        String handleUserId = processAbnormality.getHandleUserId();
        if (StringUtils.isNotBlank(updateUserId)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, updateUserId);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                processAbnormality.setUpdateUserName(userObj.toString());
            }
        } else {
            processAbnormality.setUpdateUserName("");
        }
        if (StringUtils.isNotBlank(recorderUserId)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, recorderUserId);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                processAbnormality.setRecorderUserName(userObj.toString());
            }
        } else {
            processAbnormality.setRecorderUserName("");
        }
        if (StringUtils.isNotBlank(handleUserId)) {
            Object userObj = redisService.hget(RedisKey.USER_GROUP_TYPE, handleUserId);
            if (userObj != null && StringUtils.isNotBlank(userObj.toString())) {
                processAbnormality.setHandleUserName(userObj.toString());
            }
        } else {
            processAbnormality.setHandleUserName("");
        }
    }
}
